package com.vecspace.exception;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

import com.vecspace.utils.log.HLogger;


public class RestException {
	boolean hasError = true;
	String errorCode;
	String errMsg;
	String errorDetail;
	public RestException(){
		
	}
	public RestException(Exception e){
		Throwable throwable = null;
		if(e != null){
			throwable = e;
			if(e instanceof InvocationTargetException){
				InvocationTargetException invoEx = (InvocationTargetException)e;
				throwable = invoEx.getTargetException();
			}
			hasError = true;
			
			if(throwable instanceof HException){
				HException tex = (HException)throwable;
				errorCode = tex.getErrCode();
				errMsg = tex.getMessage();
			}
			else if (throwable.getClass().getName().equals("com.dayu.ssoclient.common.HException")) {
				try {
					errMsg = BeanUtils.getProperty(throwable, "messageId");
				} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e1) {
					e1.printStackTrace();
				}
			} 
			else if(throwable != null){
				errMsg = throwable.getMessage();
				errorDetail = "";
			}
			if(errMsg == null || errMsg.length()==0){				 
				errMsg = throwable.toString();
			}
			HLogger.DEFAULT().debug("Exception:"+ errMsg);
		}
	}
	public RestException(String errorCode, String errorMessage){
		this.errorCode = errorCode;
		this.errMsg = errorMessage;
	}

	public boolean isHasError() {
		return hasError;
	}
	public void setHasError(boolean hasError) {
		this.hasError = hasError;
	}
	public RestException(String errorCode, String errorMessage, String errorDetail){
		this.errorCode = errorCode;
		this.errMsg = errorMessage;
		this.errorDetail = errorDetail;
	}
	
	
	public String getErrorCode() {
		return errorCode;
	}
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}
	public String getErrMsg() {
		return errMsg;
	}
	public String getErrorMessage() {
		return errMsg;
	}
	public void setErrMsg(String errorMessage) {
		this.errMsg = errorMessage;
	}

	public String getErrorDetail() {
		return errorDetail;
	}

	public void setErrorDetail(String errorDetail) {
		this.errorDetail = errorDetail;
	}
	
}
